package com.study.practice.structAlogrithm;

public class SingleLinkListDemo {
    public static void main(String[] args) {
        SingleLinkList linkList = new SingleLinkList();
        HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
        linkList.addHeroNodeByOrder(heroNode2);
        linkList.addHeroNodeByOrder(heroNode1);
        linkList.addHeroNodeByOrder(heroNode3);
        linkList.addHeroNodeByOrder(heroNode2);
        linkList.showList();
    }
}

class SingleLinkList {
    private HeroNode head = new HeroNode(0, "", "");

    /**
     * 尾部添加一个hero
     */
    public void addHeroNode(HeroNode node) {
        HeroNode currentNode = head;
        while (currentNode.getNext() != null) {
            currentNode = currentNode.getNext();
        }
        currentNode.setNext(node);
    }

    public void addHeroNodeByOrder(HeroNode node) {
        // 找到需要添加的位置
        /**
         * 1、找到节点的No小于node的no,node的no小于节点的next节点的no
         */
        HeroNode temp = head;
/*        while (temp != null) {
            if (temp.getNext() == null) {
                if (temp.getNo() == node.getNo()) {
                    System.out.println("添加的节点重复");
                } else {
                    temp.setNext(node);
                }
                break;
            } else {
                if (temp.getNo() == node.getNo()) {
                    System.out.println("添加的节点重复");
                    break;
                }
                if (temp.getNo() < node.getNo() && temp.getNext().getNo() > node.getNo()) {
                    node.setNext(temp.getNext());
                    temp.setNext(node);
                    break;
                }
            }
            temp = temp.getNext();
        }*/
        boolean flag = true;
        while (true) {
            if (temp.getNext() == null) {
                break;
            }
            if (temp.getNext().getNo() == node.getNo()) {
                System.out.println("添加的节点重复");
                flag = false;
                break;
            } else if (temp.getNext().getNo() > node.getNo()) {
                break;
            }
            temp = temp.getNext();
        }
        if(flag){
            node.setNext(temp.getNext());
            temp.setNext(node);
        }
    }

    /**
     * 展示单列表数据
     *
     * @param head
     */
    public void showList() {
        HeroNode currentNode = head.getNext();
        while (currentNode != null) {
            System.out.printf("no:%d, name:%s, nickName:%s\n", currentNode.getNo(), currentNode.getName(), currentNode.getNickName());
            currentNode = currentNode.getNext();
        }
    }

}


class HeroNode {
    private String name;
    private String nickName;
    private int no;
    private HeroNode next;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public HeroNode getNext() {
        return next;
    }

    public void setNext(HeroNode next) {
        this.next = next;
    }

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

}
